<template>
    <el-container>
        <el-main class="wrapper">
            <v-breadcrumb v-bind:breadcumb="breadcumb"></v-breadcrumb>
            <section class="panel" :style="{height: mainHeight+'px'}">
                <div class="panel-body">
                    <div class="btn-group" style="margin-bottom: 15px">
                        <el-button @click="create" type="primary" icon="fa fa-plus"
                                   size="small"> 新增
                        </el-button>
                        <el-button @click="list" type="primary" icon="fa fa-refresh"
                                   size="small"> 刷新
                        </el-button>
                    </div>
                    <div class="bh-btn-group">
                        <div class="bh-search">
                            <el-input size="medium" placeholder="请输入分类名称" v-model="search.name" class="input-with-select">
                                <el-button slot="append" icon="el-icon-search" @click="list"></el-button>
                            </el-input>
                        </div>
                    </div>
                    <el-table v-loading="loading"
                              element-loading-text="努力加载中..."
                              element-loading-spinner="el-icon-loading" :data="tableData" :border="true" style="width: 100%">
                        <el-table-column prop="id" label="编号" width="60px"></el-table-column>
                        <el-table-column prop="avatar" label="头像" width="100px">
                            <template slot-scope="scope">
                                <img :src="scope.row.avatar" style="height: 45px;width: 45px;border-radius: 50%"/>
                            </template>
                        </el-table-column>
                        <el-table-column prop="name" label="名称" width="200px"></el-table-column>
                        <el-table-column prop="status_text" label="状态" width="80px">
                            <template slot-scope="scope">
                                <span v-html="scope.row.status_text"></span>
                            </template>
                        </el-table-column>
                        <el-table-column prop="option_text" label="推荐" width="60px">
                            <template slot-scope="scope">
                                <span v-html="scope.row.option_text"></span>
                            </template>
                        </el-table-column>
                        <el-table-column label="操作">
                            <template slot-scope="scope">
                                <el-button type="primary" size="mini" @click="edit(scope.row)" icon="fa fa-edit" plain>
                                    编辑</el-button>
                                <el-button icon="fa fa-trash"
                                           @click="destory(scope.row)" type="danger" size="mini" plain> 删除
                                </el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <div class="pagination" >
                        <div class="pagination-total">共 <em>{{pageData.total}}</em> 个分类</div>
                        <el-pagination v-if="!loading && pageData && pageData.last_page > 1" background layout="prev, pager, next"
                                       :page-size="pageData.per_page"
                                       :total="pageData.total" :current-page="pageData.current_page"
                                       @current-change="handleCurrentChange">
                        </el-pagination>
                    </div>
                </div>
            </section>
        </el-main>
        <!-- 新增视频分类 -->
        <el-dialog
                :title="updateDialogTile"
                top="15vh"
                @open="resetForm('dialogForm')"
                :visible.sync="updateDialog"
                width="550px">
            <el-form :model="formData" size="medium" :status-icon="true" :rules="rules" ref="dialogForm" label-width="80px" class="demo-ruleForm">
                <el-form-item></el-form-item>
                <el-form-item required label="名称：" prop="name">
                    <el-input v-model="formData.name" placeholder="请填写视频分类名称"></el-input>
                </el-form-item>
                <el-form-item required label="头像：" prop="avatar">
                    <el-upload class="avatar-uploader"
                               :action="upload"
                               :show-file-list="false"
                               :headers="myHeaders"
                               :on-success="(res,file)=>{return handleAvatarSuccess(res,file,index)}"
                               :before-upload="beforeAvatarUpload" name="pic">
                        <img v-if="formData.avatar" :src="formData.avatar" class="avatar">
                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    </el-upload>
                </el-form-item>
                <el-form-item required label="状态：" prop="status">
                    <el-radio-group v-model="formData.status">
                        <el-radio border :label="1">正常</el-radio>
                        <el-radio border :label="2">锁定</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item required label="推荐：" prop="option">
                    <el-radio-group v-model="formData.option">
                        <el-radio border :label="0">不推荐</el-radio>
                        <el-radio border :label="1">推荐</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="updateDialog = false">取 消</el-button>
                <el-button type="primary" @click="update('dialogForm')">确 定</el-button>
            </span>
        </el-dialog>
    </el-container>
</template>
<script>
    import {mapGetters} from 'vuex';
    import vBreadcrumb from '../layout/breadcrumb.vue';
    import {CateList,CateDestory,CateUpdate} from '../../utils/axios';
    export default {
        components:{vBreadcrumb},
        data(){
            return {
                breadcumb:[{name: "分类管理"}],
                updateDialogTile:'',
                tableData:[],//列表数据
                pageData:[],//分页数据
                updateDialog:false,
                users: [],
                search: {name: null,page:null},//筛选
                //用户表单
                formData:{id: 0, name: '', status: 2, option: 0, avatar: ''},
                rules: {
                    name: [{ required: true, message: '请填写视频分类名称', trigger: 'blur' }],
                    avatar: [{ required: true, message: '请上传头像', trigger: 'blur' }],
                },
            }
        },
        activated(){
            this.$store.dispatch('setActive', '/cate');
            this.list();
        },
        computed: {
            ...mapGetters(['active','mainHeight','loading','upload']),
            myHeaders(){
                var token = this.$storage.get('token');
                return {
                    Authorization:token.token_type+' ' + token.access_token,
                    Accept:'application/json'
                }
            },
        },
        methods:{
            //分页
            handleCurrentChange(val) {
                this.search.page = val;
                this.list();
            },
            //获取列表
            list(){
                this.$store.dispatch('setLoading', true);
                CateList({params: this.search}).then((response)=>{
                    if (response.code) {
                        this.$message.error(response.msg);
                    } else {
                        this.$store.dispatch('setLoading', false);
                        this.tableData = response.data.data;
                        this.pageData = response.data;
                    }
                })
            },
            //编辑
            create(){
                this.formData = {id: 0, name: '', status: 1, option: 0, avatar: '',},
                this.updateDialogTile = '新增分类';
                this.updateDialog = true;
            },
            //编辑
            edit(info){
                this.formData = {
                    id: info.id,
                    name: info.name,
                    status: info.status,
                    option: info.option,
                    avatar:info.avatar
                }
                this.updateDialogTile = '编辑分类';
                this.updateDialog = true;
            },
            //更新
            update(formName){
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        CateUpdate(this.formData).then((response)=>{
                            if(response.code){
                                this.$message.error(response.msg);
                            }else{
                                this.$message({
                                    type: 'success',
                                    duration: '1000',
                                    message: response.msg,
                                    onClose:() =>{
                                        this.updateDialog = false;
                                        this.list();
                                    }
                                });
                            }
                        });
                    }
                });
            },
            //删除
            destory(info){
                this.$confirm('确定要删除信息吗？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    CateDestory({params:{id: info.id}}).then((response) => {
                        if (response.code) {
                            this.$message.error(response.msg);
                        } else {
                            this.list();
                        }
                    });
                }).catch(() => {});
            },
            //重置表单
            resetForm(formName) {
                if (this.$refs[formName]!==undefined) {
                    this.$refs[formName].resetFields();
                }
            },
            handleAvatarSuccess(res) {
                this.formData.avatar = res.data.path;
            },
            beforeAvatarUpload(file) {
                const isJPG = file.type === 'image/jpeg';
                const isPNG = file.type === 'image/png';
                const isLt1M = file.size / 1024 / 1024 < 1;
                if (!isJPG && !isPNG) {
                    this.$message.error('上传图片只能是JPG/PNG格式!');
                }
                if (!isLt1M) {
                    this.$message.error('上传图片大小不能超过 1MB!');
                }
                return (isJPG || isPNG) && isLt1M;
            }
        },
    }
</script>